package com.powergisol.gis.controller;

import com.powergisol.gis.dto.ZkTcSwDTO;
import com.powergisol.gis.exception.CommonEnum;
import com.powergisol.gis.exception.GlobalExceptionHandler;
import com.powergisol.gis.service.TabZuanKongService;
import com.powergisol.gis.util.Result;
import com.powergisol.gis.vo.ZKAddVO;
import com.powergisol.gis.vo.ZkTcSwVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.CollectionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/zuankong")
@Slf4j
public class TabZuanKongController {
    @Autowired
    TabZuanKongService tabZuanKongService;

    /**
     * 钻孔列表接口
     *
     * @param serviceIds 服务ID数组
     * @return
     */
    @GetMapping("/getzkdata")
    public Result getZKData(@RequestParam("serviceIds") Integer[] serviceIds) {
        long begin = System.currentTimeMillis();
        Map<String, ZkTcSwVO> zkData = this.tabZuanKongService.getZKData(serviceIds);
        Result result = new Result();
        result.success(zkData);
        log.info("钻孔列表查询耗时{}", (System.currentTimeMillis() - begin));
        return result;
    }

    /**
     * 钻孔新增
     *
     * @param vo
     * @return
     */
    @ApiOperation(value = "钻孔新增", notes = "钻孔新增")
    @PostMapping(value = "/addzk")
    public Result addZuanKong(@RequestBody ZKAddVO vo) {
//        public Result addZuanKong(String zkAddVO) {
//        ObjectMapper mapper = new ObjectMapper();
//        ZKAddVO vo = null;
//        try {
//            vo = mapper.readValue(zkAddVO, ZKAddVO.class);
//        } catch (IOException ioException) {
//            ioException.printStackTrace();
//        }
        return tabZuanKongService.addZK(vo);
    }

    @ApiOperation(value = "钻孔编辑", notes = "钻孔编辑")
    @PostMapping(value = "/updatezk")
    public Result updateZuanKong(@RequestBody ZKAddVO vo) {
//        System.out.println(zkAddVO);
//        ObjectMapper mapper = new ObjectMapper();
//        ZKAddVO zkaddvo = null;
//        try {
//            zkaddvo = mapper.readValue(zkAddVO, ZKAddVO.class);
//        } catch (IOException ioException) {
//            ioException.printStackTrace();
//        }
        return tabZuanKongService.updateZK(vo);
    }

    /**
     * 获取钻孔的所有类型
     *
     * @return
     */
    @RequestMapping(value = "/getallzkclass", method = RequestMethod.GET)
    public Result getAllZKClass() {
        List<String> zkClass = tabZuanKongService.getAllClass();
        return new Result().success(zkClass);
    }

    /**
     * 钻孔详情
     *
     * @param zkTcSwDTO
     * @return
     */
    @RequestMapping(value = "/getdetails", method = RequestMethod.POST)
    public Result getDetails(@RequestBody ZkTcSwDTO zkTcSwDTO) {
//        ObjectMapper mapper = new ObjectMapper();
//        ZkTcSwDTO zkTcSwDTO1 = null;
//        try {
//            zkTcSwDTO1 = mapper.readValue(zkTcSwDTO, ZkTcSwDTO.class);
//        } catch (IOException ioException) {
//            ioException.printStackTrace();
//        }
        return new Result().success(tabZuanKongService.getDetails(zkTcSwDTO));
    }

    /**
     * 钻孔删除
     *
     * @param zkTcSwDTOList
     * @return
     */
    @RequestMapping(value = "/deletezuankong", method = RequestMethod.POST)
    public Result deleteZuanKong(@RequestBody List<ZkTcSwDTO> zkTcSwDTOList) {

//        ObjectMapper mapper = new ObjectMapper();
//        CollectionType collectionType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, ZkTcSwDTO.class);
//        List<ZkTcSwDTO> zkTcSwDTOList = null;
//        try {
//            zkTcSwDTOList = mapper.readValue(zkTcSwDTO, collectionType);
//        } catch (IOException ioException) {
//            ioException.printStackTrace();
//            throw new GlobalExceptionHandler(CommonEnum.INTERNAL_SERVER_ERROR);
//        }

        return tabZuanKongService.deleteZK(zkTcSwDTOList);
    }

}
